﻿using log4net;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Text;

namespace DataBaseLog
{
    public delegate void UIMessageEventHandler(string info, MessageType type);

    public enum MessageType
    {
        INFO,
        ERROR,
        DEBUG
    }

    public class Log
    {
        private static readonly ILog lInfo = LogManager.GetLogger("Log");
        private static readonly ILog lErr = LogManager.GetLogger("Err");
        private static readonly ILog lDebug = LogManager.GetLogger("Debug");
        public static event UIMessageEventHandler UiLogReceived;
        public static void WriteToGui()
        {
            log4net.Config.XmlConfigurator.Configure();
        }
        static string outInfo = string.Empty;
        //public static void Info(string msg)
        //{
        //    lInfo.Info(msg);
        //}

        public static void Info(string msg, bool isUiDisplayed)
        {
            lInfo.Info(msg);
            if (isUiDisplayed)
            {
                UiLogReceived?.Invoke(msg, MessageType.INFO);
            }
        }

        static string outMsg = string.Empty;
        public static void Error(string msg)
        {
            lErr.Error(msg);
        }

        public static void Error(string msg, bool isUiDisplayed)
        {
            lErr.Error(msg);
            if (isUiDisplayed)
            {
                UiLogReceived?.Invoke(msg, MessageType.ERROR);
            }
        }

        public static void Debug(string msg, bool isUiDisplayed)
        {
            lDebug.Debug(msg);
            if (isUiDisplayed)
            {
                UiLogReceived?.Invoke(msg, MessageType.DEBUG);
            }
        }

        public static void Error(string msg, bool isUiDisplayed, Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" " + msg);
            sb.Append(" " + ex.ToString());
            lErr.Error(sb.ToString());
            if (isUiDisplayed)
            {
                UiLogReceived?.Invoke(msg, MessageType.ERROR);
            }
        }

        public static void Error(Exception ex)
        {
            lErr.Error(ex.ToString());
        }

        public static void Debug(string msg)
        {
            lDebug.Debug(msg);
        }

        public class UiLogEventArgs : EventArgs
        {
            public string Message { get; private set; }
            public UiLogEventArgs(string message)
            {
                Message = message;
            }
        }
    }
}
